package com.kalvin.kvf.modules.dy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.modules.dy.entity.*;
import com.kalvin.kvf.modules.dy.mapper.*;
import com.kalvin.kvf.modules.dy.service.LoginUserService;
import com.kalvin.kvf.modules.hxy.entity.Account;
import com.kalvin.kvf.modules.hxy.entity.Port;
import com.kalvin.kvf.modules.hxy.mapper.AccountMapper;
import com.kalvin.kvf.modules.hxy.mapper.PortMapper;
import javafx.scene.web.WebEngine;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PreDestroy;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("dy/crawler")
public class CrawlerController extends BaseController {
    //刘上传测试

    @Autowired
    private PortMapper portMapper;
    @Autowired
    private LoginUserService loginUserService;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private IntentionCustMapper intentionCustMapper;

    @Autowired
    private LoginUserMapper loginUserMapper;

    @Autowired
    private BloggersMapper bloggersMapper;
    @Autowired
    private BloggersVideoMapper bloggersVideoMapper;

    /**
     * 爬虫
     *
     * @return
     */
    @PostMapping(value = "add")
    //@Scheduled(fixedRate = 100000)
    public R crawler() throws Exception {
        //获取任务搜索源
        Task task = taskMapper.selectOne(new QueryWrapper<Task>().eq("task_status", 0).last("limit 1"));
        if (task == null) {
            throw new Exception("没有可执行任务");
        }
        //获取端口号
        Port port = portMapper.selectOne(new QueryWrapper<Port>().eq("status", 0).last("limit 1"));
        if (port == null) {
            throw new Exception("当前运行浏览器已达上限");
        }
        QueryWrapper<LoginUser> userWrapper = new QueryWrapper<>();
        userWrapper.lambda().eq(LoginUser::getIsLogin, 0);
        userWrapper.lambda().eq(LoginUser::getStatus,0);
        userWrapper.lambda().orderByAsc(LoginUser::getLastLoginTime);
        userWrapper.last("limit 1");
        LoginUser loginUser2 = loginUserMapper.selectOne(userWrapper);
        //没有可用登录账号时 不打开浏览器
        if (StringUtils.isEmpty(loginUser2)) {
            throw new Exception("无可用账号,请稍后重试");
        }
        Map<String, Object> map = loginUserService.useCookiesLogin(port, loginUser2);
        //获取当前用户信息
        Account account = accountMapper.selectById(task.getAccountId());
        Object dyAccount = map.get("msg");
        LoginUser login = loginUserService.getOne(new QueryWrapper<LoginUser>().eq("account_number", dyAccount));
        //获取driver对象
        WebDriver driver = (WebDriver) map.get("driver");
        //判断有没有登录成功
//        try {
//            driver.findElement(By.cssSelector("#douyin-header > div.oJArD0aS > header > div > div > div.iqHX00br > div > div > div:nth-child(7) > div > a > div > img"));
//        } catch (Exception e) {
//            System.out.println("登录账号存在异常，无法正常执行！");
//            driver.close();
//            driver.quit();
//            //设置当前账号登录状态为1异常
//            loginUser2.setStatus(1);
//            loginUserService.updateById(loginUser2);
//        }
        //跳转到搜索链接
        driver.get("https://www.douyin.com/search/" + task.getSearchKeyword());
        Thread.sleep(7000);
        //点击视频按钮
        try {
            WebElement button = driver.findElement(By.cssSelector("#search-content-area > div > div.GwBBBRKQ > div.qB3P4FP2 > div.dC4GYZQ1 > span:nth-child(2)"));
            button.click();
        } catch (Exception e) {
            System.out.println("跳出验证码,请处理");
            driver.close();
            driver.quit();
        }
        Thread.sleep(5000);
        //修改该抖音用户的登录状态  为  登陆中
        login.setIsLogin(1);
        //更新上次登录时间
        login.setLastLoginTime(LocalDateTime.now());
        loginUserService.update(login, new QueryWrapper<LoginUser>().eq("account_number", dyAccount));
        //修改状态为 已使用
        port.setStatus(1);
        portMapper.updateById(port);
        //修改任务状态为运行中
        task.setTaskStatus(1);
        taskMapper.updateById(task);
        //视频列表下拉
        for (int i = 1; i <= 1; i++) {
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
            Thread.sleep(1000);
        }
        Thread.sleep(2000);
        //获取视频详情链接
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"search-content-area\"]/div/div[3]/div[2]/ul/li"));

        for (WebElement page : elements) {
            try {
                WebElement label = page.findElement(By.cssSelector(".swoZuiEM"));
                WebElement element = page.findElement(By.cssSelector("a"));
                String href = element.getAttribute("href");
                Video video = new Video();
                video.setTaskId(task.getId());
                video.setVideoLink(href);
                video.setPhone(account.getPhone());
                video.setTaskName(task.getName());
                video.setCreateTime(LocalDateTime.now());
                video.setTaskName(task.getName());
                video.setLabel(label.getText());
                videoMapper.insert(video);
            } catch (Exception e) {
                System.out.println("获取视频链接有误 跳出 继续执行");
            }
        }
        Integer taskCount = videoMapper.selectCount(new QueryWrapper<Video>().eq("task_id", task.getId()));
        task.setVideoNum(taskCount);
        taskMapper.updateById(task);
        List<Video> videoList = videoMapper.selectList(new QueryWrapper<Video>().eq("task_id", task.getId()));
        //爬取当前任务评论
        for (Video video : videoList) {
            try {
                driver.get(video.getVideoLink());
                Thread.sleep(5000);
                WebElement pl = driver.findElement(By.id("douyin-right-container"));
                Thread.sleep(1000);
                //获取总评论数
                int a = 0;
                int b = 1;
                try {
                    //纯视频评论数量`
                    WebElement ce7XkkTw = driver.findElement(By.xpath("//*[@id=\"douyin-right-container\"]/div[2]/div/div[1]/div[3]/div/div[2]/div[1]/div[2]/span"));
                    String str = ce7XkkTw.getText();
                    String replace = str.replace(".", "0");
                    String replace2 = replace.replace("万", "00");
                    a = Integer.parseInt(replace2);
                    System.out.println("纯视频成功");
                } catch (Exception e) {
                    System.out.println("获取纯视频失败 跳出继续向下执行");
                }
                try {
                    //图文视频评论数量
                    WebElement ce7XkkTw = driver.findElement(By.xpath("//*[@id=\"douyin-right-container\"]/div[2]/main/div[1]/div[2]/div/div[1]/div[2]/span"));
                    String str = ce7XkkTw.getText();
                    String replace = str.replace(".", "0");
                    String replace2 = replace.replace("万", "00");
                    a = Integer.parseInt(replace2);
                    System.out.println("图文视频成功");
                } catch (Exception e) {
                    System.out.println("获取图文视频失败 跳出继续向下执行");
                }
                if (a >= 400) {
                    System.out.println("我是>=");
                    b = 40;
                }
                if (a <= 400) {
                    System.out.println("我是<=");
                    b = (int) Math.ceil((double) a / (double) 10);
                }
                //下拉加载评论
                for (int i = 1; i <= b; i++) {
                    ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
                    Thread.sleep(1000);
                }
                //获取详情评论
                List<WebElement> pinglun = driver.findElements(By.xpath("//*[@id=\"douyin-right-container\"]/div[2]/div/div[1]/div[5]/div/div/div[3]/div"));

                for (WebElement webElement : pinglun) {
                    System.out.println(webElement.getText());
                    try {

                        //WebElement yongHuName = webElement.findElement(By.xpath("//*[@id=\"douyin-right-container\"]/div[2]/div/div[1]/div[5]/div/div/div[3]/div"));


                        //获取用户名称
                        WebElement yongHuNameElement = webElement.findElement(By.cssSelector("span > span > span > span > span > span"));
                        System.out.println(yongHuNameElement.getText());
                        //用户头像
                        WebElement touxiang = webElement.findElement(By.className("PbpHcHqa"));
                        System.out.println(touxiang.getText());

                        String src = touxiang.getAttribute("src");
                        System.out.println(src);
                        //评论时间
                        WebElement plTime = webElement.findElement(By.className("L4ozKLf7"));
                        WebElement plTimeElement = plTime.findElement(By.cssSelector("span"));
                        System.out.println(plTimeElement.getText());
                        //遍历评论
                        WebElement element = webElement.findElement(By.cssSelector("span > span > span > span > span > span > span"));
                        String text = element.getText();
                        System.out.println(text);
                        //每个评论的用户页
                        WebElement plUser = webElement.findElement(By.cssSelector("a"));
                        String href = plUser.getAttribute("href");
                        //取出关键字
                        String siftKeywords = task.getSiftKeywords();
                        String[] siftKeywordsList = siftKeywords.split(",");
                        boolean siftContains = false;
                        //获取命中关键词
                        String keyword = null;
                        for (String s : siftKeywordsList) {
                            siftContains = text.contains(s);
                            if (siftContains) {
                                keyword = s;
                                break;
                            }
                        }
                        if (siftContains && task.getShieldKeywords() == null || siftContains && task.getShieldKeywords().equals("")) {
                            //保存数据到客户库
                            IntentionCust intentionCust = new IntentionCust();
                            intentionCust.setTaskId(task.getId());
                            intentionCust.setVideoLink(video.getVideoLink());
                            //intentionCust.setNickname(null);//意向客户网名
                            intentionCust.setContent(text);
                            intentionCust.setPhone(account.getPhone());//使用的客户手机号
                            //intentionCust.setVx(null);//意向客户微信
                            intentionCust.setHitKeyword(keyword);
                            intentionCust.setAnalyseTime(LocalDateTime.now());
                            intentionCust.setCreateTime(LocalDateTime.now());
                            intentionCust.setVideoId(video.getId());
                            if (yongHuNameElement.getText() != null && !yongHuNameElement.getText().equals("")) {
                                intentionCust.setNickname(yongHuNameElement.getText());
                            }
                            if (plTimeElement.getText() != null && !plTimeElement.getText().equals("")) {
                                intentionCust.setConsuTime(plTimeElement.getText());
                            }
                            intentionCust.setCustHome(href);
                            intentionCust.setTaskName(task.getName());
                            if (video.getLabel() != null && !video.getLabel().equals("")) {
                                intentionCust.setLabel(video.getLabel());
                            }
                            intentionCust.setCustAvatar(src);
                            //intentionCust.setCusPhone(null);//意向客户手机号
                            intentionCustMapper.insert(intentionCust);
                        }
                        if (siftContains && task.getShieldKeywords() != null && !task.getShieldKeywords().equals("")) {
                            //取出屏蔽关键词
                            String shieldKeywords = task.getShieldKeywords();
                            String[] shieldKeywordsList = shieldKeywords.split(",");
                            boolean shieldContains = false;
                            for (String s : shieldKeywordsList) {
                                shieldContains = text.contains(s);
                                if (shieldContains) {
                                    break;
                                }
                            }
                            if (!shieldContains) {
                                //保存数据到客户库
                                IntentionCust intentionCust = new IntentionCust();
                                intentionCust.setTaskId(task.getId());
                                intentionCust.setVideoLink(video.getVideoLink());
                                //intentionCust.setNickname(null);//意向客户网名
                                intentionCust.setContent(text);
                                intentionCust.setPhone(account.getPhone());//使用的客户手机号
                                //intentionCust.setVx(null);//意向客户微信
                                intentionCust.setHitKeyword(keyword);
                                intentionCust.setAnalyseTime(LocalDateTime.now());
                                intentionCust.setCreateTime(LocalDateTime.now());
                                intentionCust.setVideoId(video.getId());
                                if (yongHuNameElement.getText() != null && !yongHuNameElement.getText().equals("")) {
                                    intentionCust.setNickname(yongHuNameElement.getText());
                                }
                                if (plTimeElement.getText() != null && !plTimeElement.getText().equals("")) {
                                    intentionCust.setConsuTime(plTimeElement.getText());
                                }
                                intentionCust.setCustHome(href);
                                intentionCust.setTaskName(task.getName());
                                if (video.getLabel() != null && !video.getLabel().equals("")) {
                                    intentionCust.setLabel(video.getLabel());
                                }
                                intentionCust.setCustAvatar(src);
                                //intentionCust.setCusPhone(null);//意向客户手机号
                                intentionCustMapper.insert(intentionCust);
                            }
                        }
                        Integer count = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", task.getId()));
                        if (Objects.equals(count, task.getCustNum())) {
                            System.out.println("准备跳出循环");
                            break;
                        }
                    } catch (Exception e) {
                        System.out.println("异常跳出 继续执行" + e);
                    }
                }
                Integer count = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", task.getId()));
                if (Objects.equals(count, task.getCustNum())) {
                    System.out.println("准备跳出循环");
                    break;
                }
                Integer videoCount = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("video_id", video.getId()));
                video.setVideoCustNum(videoCount);
                video.setDiggingTime(LocalDateTime.now());
                videoMapper.updateById(video);
            } catch (Exception e) {
                System.out.println("遍历某个视频出错，跳出当前错误 继续执行" + e);
            }
        }
        //统计已获取客户数量
        Integer count = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", task.getId()));
        task.setUseCustNum(count);
        taskMapper.updateById(task);
        //如果当前用户的点数不为null 则减掉对应的点数
        if (account.getCustNum() != null && account.getCustNum() > 0) {
            Account account2 = accountMapper.selectById(task.getAccountId());
            Integer a = account2.getCustNum() - count;
            account2.setCustNum(a);
            accountMapper.updateById(account2);
        }
        //关闭当前浏览器窗口
        driver.close();
        //关闭进程
        driver.quit();
        //修改端口号为未使用
        port.setStatus(0);
        portMapper.updateById(port);
        //运行完成修改当前抖音号状态为 未登录状态
        Object mag = map.get("msg");
        LoginUser loginUser = loginUserService.getOne(new QueryWrapper<LoginUser>().eq("account_number", mag));
        loginUser.setIsLogin(0);
        loginUserService.update(loginUser, new QueryWrapper<LoginUser>().eq("account_number", mag));
        return R.ok();
    }

    /**
     * 同行博主分析
     *
     * @return
     * @throws Exception
     */
    @PostMapping(value = "bloggers")
    public R bloggers() throws Exception {
        //获取任务搜索源
        Bloggers bloggers = bloggersMapper.selectOne(new QueryWrapper<Bloggers>().eq("task_status", 0).last("limit 1"));
        if (bloggers == null) {
            throw new Exception("没有可执行任务");
        }
        //获取端口号
        Port port = portMapper.selectOne(new QueryWrapper<Port>().eq("status", 0).last("limit 1"));
        if (port == null) {
            throw new Exception("当前运行浏览器已达上限");
        }
        QueryWrapper<LoginUser> userWrapper = new QueryWrapper<>();
        userWrapper.lambda().eq(LoginUser::getIsLogin, 0);
        userWrapper.lambda().orderByAsc(LoginUser::getLastLoginTime);
        userWrapper.last("limit 1");
        LoginUser loginUser2 = loginUserMapper.selectOne(userWrapper);
        //没有可用登录账号时 不打开浏览器
        if (StringUtils.isEmpty(loginUser2)) {
            throw new Exception("无可用账号,请稍后重试");
        }
        Map<String, Object> map = loginUserService.useCookiesLogin(port, loginUser2);
        //获取当前用户信息
        Account account = accountMapper.selectById(bloggers.getAccountId());
        Object dyAccount = map.get("msg");
        LoginUser login = loginUserService.getOne(new QueryWrapper<LoginUser>().eq("account_number", dyAccount));
        //获取driver对象
        WebDriver driver = (WebDriver) map.get("driver");
        //判断有没有登录成功
        try {
            driver.findElement(By.cssSelector("#douyin-header > div.oJArD0aS > header > div > div > div.iqHX00br > div > div > div:nth-child(7) > div > a > div > img"));
        } catch (Exception e) {
            System.out.println("登录账号存在异常，无法正常执行！");
            driver.close();
            driver.quit();
            //设置当前账号登录状态为1异常
            loginUser2.setStatus(1);
            loginUserService.updateById(loginUser2);
        }
        driver.get(bloggers.getBloggersUrl());
        Thread.sleep(3000);
        //修改该抖音用户的登录状态  为  登陆中
        login.setIsLogin(1);
        //更新上次登录时间
        login.setLastLoginTime(LocalDateTime.now());
        loginUserService.update(login, new QueryWrapper<LoginUser>().eq("account_number", dyAccount));
        //修改状态为 已使用
        port.setStatus(1);
        portMapper.updateById(port);
        //修改任务状态为运行中
        bloggers.setTaskStatus(1);
        bloggersMapper.updateById(bloggers);
        //视频页下拉刷新
        for (int i = 1; i <= 15; i++) {
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
            Thread.sleep(1000);
        }
        //获取视频链接
        List<WebElement> shiPinList = driver.findElements(By.xpath("//*[@id=\"douyin-right-container\"]/div[2]/div/div/div[3]/div/div/div[2]/div[2]/div[2]/ul/li"));
        //
        for (WebElement page : shiPinList) {
            WebElement element = page.findElement(By.cssSelector("a"));
            String href = element.getAttribute("href");
            System.out.println(href);
            //获取视频标签
            WebElement label = element.findElement(By.cssSelector(".__0w4MvO"));
            BloggersVideo bloggersVideo = new BloggersVideo();
            bloggersVideo.setTaskBloggersId(bloggers.getId());
            bloggersVideo.setPhone(bloggers.getPhone());
            bloggersVideo.setVideoLink(href);
            bloggersVideo.setTaskName(bloggers.getName());
            bloggersVideo.setCreateTime(LocalDateTime.now());
            bloggersVideo.setLabel(label.getText());
            System.out.println(label.getText());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
            String id = sdf.format(System.currentTimeMillis());
            bloggersVideo.setId(Long.valueOf(id));
            bloggersVideoMapper.insert(bloggersVideo);
        }

        Integer count = bloggersVideoMapper.selectCount(new QueryWrapper<BloggersVideo>().eq("task_bloggers_id", bloggers.getId()));
        bloggers.setVideoNum(count);
        bloggersMapper.updateById(bloggers);
        List<BloggersVideo> videoList = bloggersVideoMapper.selectList(new QueryWrapper<BloggersVideo>().eq("task_bloggers_id", bloggers.getId()));
        for (BloggersVideo bloggersVideo : videoList) {
            try {
                driver.get(bloggersVideo.getVideoLink());
                Thread.sleep(2000);
                WebElement pl = driver.findElement(By.id("douyin-right-container"));
                Thread.sleep(1000);
                //获取总评论数
                int a = 0;
                int b = 1;
                try {
                    //纯视频评论数量
                    WebElement ce7XkkTw = driver.findElement(By.cssSelector("#douyin-right-container > div:nth-child(2) > div > div.leftContainer.w0R6mo9z > div.nIyi1iFP > div > div.a30pLzw4 > div.UwvcKsMK > div:nth-child(2) > span"));
                    String str = ce7XkkTw.getText();
                    String replace = str.replace(".", "0");
                    String replace2 = replace.replace("万", "00");
                    a = Integer.parseInt(replace2);
                    System.out.println("纯视频成功");
                } catch (Exception e) {
                    System.out.println("获取纯视频失败 跳出继续向下执行");
                }
                try {
                    //图文视频评论数量
                    WebElement ce7XkkTw = driver.findElement(By.xpath("//*[@id=\"douyin-right-container\"]/div[2]/main/div[1]/div[2]/div/div[1]/div[2]/span"));
                    String str = ce7XkkTw.getText();
                    String replace = str.replace(".", "0");
                    String replace2 = replace.replace("万", "00");
                    a = Integer.parseInt(replace2);
                    System.out.println("图文视频成功");
                } catch (Exception e) {
                    System.out.println("获取图文视频失败 跳出继续向下执行");
                }
                if (a >= 400) {
                    System.out.println("我是>=");
                    b = 40;
                }
                if (a <= 400) {
                    System.out.println("我是<=");
                    b = (int) Math.ceil((double) a / (double) 10);
                }
                //下拉加载评论
                for (int i = 1; i <= b; i++) {
                    ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
                    Thread.sleep(1000);
                }
                //获取详情评论
                List<WebElement> pinglun = pl.findElements(By.cssSelector(".CDx534Ub"));
                for (WebElement webElement : pinglun) {
                    try {
                        //获取用户名称
                        WebElement yongHuName = webElement.findElement(By.cssSelector(".Uvaas5kD"));
                        WebElement yongHuNameElement = yongHuName.findElement(By.cssSelector("span > span > span > span > span > span"));
                        //用户头像
                        WebElement touxiang = webElement.findElement(By.className("PbpHcHqa"));
                        String src = touxiang.getAttribute("src");
                        //评论时间
                        WebElement plTime = webElement.findElement(By.cssSelector(".L4ozKLf7"));
                        WebElement plTimeElement = plTime.findElement(By.cssSelector("span"));

                        WebElement plDiv = webElement.findElement(By.cssSelector(".a9uirtCT"));
                        //遍历评论
                        WebElement element = plDiv.findElement(By.cssSelector("span span span span span span"));
                        String text = element.getText();
                        //每个评论的用户页
                        WebElement plUser = webElement.findElement(By.cssSelector("a"));
                        String href = plUser.getAttribute("href");
                        //取出关键字
                        String siftKeywords = bloggers.getSiftKeywords();
                        String[] siftKeywordsList = siftKeywords.split(",");
                        boolean siftContains = false;
                        //获取命中关键词
                        String keyword = null;
                        for (String s : siftKeywordsList) {
                            siftContains = text.contains(s);
                            if (siftContains) {
                                keyword = s;
                                break;
                            }
                        }
                        if (siftContains && bloggers.getShieldKeywords() == null || siftContains && bloggers.getShieldKeywords().equals("")) {
                            //保存数据到客户库
                            IntentionCust intentionCust = new IntentionCust();
                            intentionCust.setTaskId(bloggers.getId());
                            intentionCust.setVideoLink(bloggersVideo.getVideoLink());
                            //intentionCust.setNickname(null);//意向客户网名
                            intentionCust.setContent(text);
                            intentionCust.setPhone(account.getPhone());//使用的客户手机号
                            //intentionCust.setVx(null);//意向客户微信
                            intentionCust.setHitKeyword(keyword);
                            intentionCust.setAnalyseTime(LocalDateTime.now());
                            intentionCust.setCreateTime(LocalDateTime.now());
                            intentionCust.setVideoId(bloggersVideo.getId());
                            if (yongHuNameElement.getText() != null && !yongHuNameElement.getText().equals("")) {
                                intentionCust.setNickname(yongHuNameElement.getText());
                            }
                            if (plTimeElement.getText() != null && !plTimeElement.getText().equals("")) {
                                intentionCust.setConsuTime(plTimeElement.getText());
                            }
                            intentionCust.setCustHome(href);
                            intentionCust.setTaskName(bloggers.getName());
                            if (bloggersVideo.getLabel() != null && !bloggersVideo.getLabel().equals("")) {
                                intentionCust.setLabel(bloggersVideo.getLabel());
                            }
                            intentionCust.setCustAvatar(src);
                            //intentionCust.setCusPhone(null);//意向客户手机号
                            intentionCustMapper.insert(intentionCust);
                        }
                        if (siftContains && bloggers.getShieldKeywords() != null && !bloggers.getShieldKeywords().equals("")) {
                            //取出屏蔽关键词
                            String shieldKeywords = bloggers.getShieldKeywords();
                            String[] shieldKeywordsList = shieldKeywords.split(",");
                            boolean shieldContains = false;
                            for (String s : shieldKeywordsList) {
                                shieldContains = text.contains(s);
                                if (shieldContains) {
                                    break;
                                }
                            }
                            if (!shieldContains) {
                                //保存数据到客户库
                                IntentionCust intentionCust = new IntentionCust();
                                intentionCust.setTaskId(bloggers.getId());
                                intentionCust.setVideoLink(bloggersVideo.getVideoLink());
                                //intentionCust.setNickname(null);//意向客户网名
                                intentionCust.setContent(text);
                                intentionCust.setPhone(account.getPhone());//使用的客户手机号
                                //intentionCust.setVx(null);//意向客户微信
                                intentionCust.setHitKeyword(keyword);
                                intentionCust.setAnalyseTime(LocalDateTime.now());
                                intentionCust.setCreateTime(LocalDateTime.now());
                                intentionCust.setVideoId(bloggersVideo.getId());
                                if (yongHuNameElement.getText() != null && !yongHuNameElement.getText().equals("")) {
                                    intentionCust.setNickname(yongHuNameElement.getText());
                                }
                                if (plTimeElement.getText() != null && !plTimeElement.getText().equals("")) {
                                    intentionCust.setConsuTime(plTimeElement.getText());
                                }
                                intentionCust.setCustHome(href);
                                intentionCust.setTaskName(bloggers.getName());
                                if (bloggersVideo.getLabel() != null && !bloggersVideo.getLabel().equals("")) {
                                    intentionCust.setLabel(bloggersVideo.getLabel());
                                }
                                intentionCust.setCustAvatar(src);
                                //intentionCust.setCusPhone(null);//意向客户手机号
                                intentionCustMapper.insert(intentionCust);
                            }
                        }
                        Integer count2 = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", bloggers.getId()));
                        if (Objects.equals(count2, bloggers.getCustNum())) {
                            System.out.println("准备跳出循环");
                            break;
                        }
                    } catch (Exception e) {
                        System.out.println("异常跳出 继续执行" + e);
                    }
                }
                Integer count2 = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", bloggers.getId()));
                if (Objects.equals(count2, bloggers.getCustNum())) {
                    System.out.println("准备跳出循环");
                    break;
                }
                Integer videoCount = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("video_id", bloggersVideo.getId()));
                bloggersVideo.setVideoCustNum(videoCount);
                bloggersVideo.setDiggingTime(LocalDateTime.now());
                bloggersVideoMapper.updateById(bloggersVideo);
            }catch (Exception e) {
                System.out.println("遍历某个视频出错，跳出当前错误 继续执行" + e);
            }
        }
        //统计已获取客户数量
        Integer count3 = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("task_id", bloggers.getId()));
        bloggers.setUseCustNum(count3);
        bloggersMapper.updateById(bloggers);
        //如果当前用户的点数不为null 则减掉对应的点数
        if (account.getCustNum() != null && account.getCustNum() > 0) {
            Account account2 = accountMapper.selectById(bloggers.getAccountId());
            Integer a = account2.getCustNum() - count;
            account2.setCustNum(a);
            accountMapper.updateById(account2);
        }
        driver.close();
        driver.quit();
        //修改端口号为未使用
        port.setStatus(0);
        portMapper.updateById(port);
        //运行完成修改当前抖音号状态为 未登录状态
        Object mag = map.get("msg");
        LoginUser loginUser = loginUserService.getOne(new QueryWrapper<LoginUser>().eq("account_number", mag));
        loginUser.setIsLogin(0);
        loginUserService.update(loginUser, new QueryWrapper<LoginUser>().eq("account_number", mag));
        return R.ok();
    }

}
